package com.ostrich.manpower.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.map.LinkedMap;
import org.apache.log4j.Logger;
import org.jsoup.helper.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.util.HtmlUtil;
import com.base.util.Monthutil;
import com.base.util.SessionUtils;
import com.base.web.BaseAction;
import com.jeecg.entity.SysUser;
import com.ostrich.manpower.bean.TostHrAccfundinfo;
import com.ostrich.manpower.bean.TostHrBaseinfo;
import com.ostrich.manpower.bean.TostHrCheckinlog;
import com.ostrich.manpower.bean.TostHrMealaddinfo;
import com.ostrich.manpower.bean.TostHrSalaryinfo;
import com.ostrich.manpower.bean.TostHrSocialsecurity;
import com.ostrich.manpower.bean.TostHrSubsidyinfo;
import com.ostrich.manpower.common.DateUtil;
import com.ostrich.manpower.service.impl.TostHrAccfundinfoService;
import com.ostrich.manpower.service.impl.TostHrBaseinfoService;
import com.ostrich.manpower.service.impl.TostHrCheckinlogService;
import com.ostrich.manpower.service.impl.TostHrMealaddinfoService;
import com.ostrich.manpower.service.impl.TostHrSalaryinfoService;
import com.ostrich.manpower.service.impl.TostHrSalaryinfoTService;
import com.ostrich.manpower.service.impl.TostHrSocialsecurityService;
import com.ostrich.manpower.service.impl.TostHrSubsidyinfoService;

/**
 * ClassName: Controller <br/>
 * date: 2013-5-9 下午03:07:38 <br/>
 * 
 * @author wangyinan
 * @version
 */
@Controller
@RequestMapping("/tostHrSalaryinfoT")
public class TostHrSalaryinfoTController extends BaseAction {
	private final static Logger log = Logger
			.getLogger(TostHrSalaryinfoTController.class);
	// Servrice start
	@Autowired(required = false)
	private TostHrBaseinfoService tosthrbaseinfo;
	
	@Autowired(required = false)
	// 自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private TostHrSalaryinfoTService hrsalaryinfotservice;
	
	@Autowired(required = false)
	private TostHrSalaryinfoService  hrsalaryinfoservic;
	
	//补贴
	@Autowired(required = false)
	private TostHrSubsidyinfoService hrsubsidyservice;
	
	//餐补
	@Autowired(required = false)
	private TostHrMealaddinfoService hrmealaddservice;
	
	//考勤
	@Autowired(required = false)
	private TostHrCheckinlogService  hrcheckinlogservice; 

   //社保
	@Autowired(required = false)
	private TostHrSocialsecurityService hrsocialsecurityservice;
	
   //工资
	@Autowired(required = false)
	private TostHrSalaryinfoService  tosthrsalaryinfoservice;
	
	//公积金
	@Autowired(required = false)
	private TostHrAccfundinfoService tosthraccfundinfoservice;
	//基础信息
	@Autowired(required = false)
	private TostHrBaseinfoService  tosthrbaseinfoservice;
	private String zStartM = null;
	private String zEndM =null;
	private String sStartM = null;
	private String sEndM =null;
	private List<TostHrSalaryinfo> datalist;
	Map<Object, List<TostHrMealaddinfo>> jsonMap = new HashMap<Object, List<TostHrMealaddinfo>>();
	
	//补贴表中的相关补贴
	Float hsbEducation = 0.0f;
	Float hsbComputer = 0.0f;
	Float hsbKnowadmin = 0.0f;
	Float hsbExpert = 0.0f;
	Float hsbTraffic = 0.0f;
	Float hsbCommunication = 0.0f;
	Float hsbRemote = 0.0f;
	Float hsbOthersubs = 0.0f;
	Float hciDeductionsTotal = 0.0f;
	Float hsaPaid = 0.0f;
	Float hacFactamount = 0.0f;
	Float hmeMealsubsidies =0.0f;
	Float hmeMealeduction = 0.0f;
	Float hsoTotal = 0.0f;
	private TostHrBaseinfo baseinfo = new TostHrBaseinfo();
	/**
	 * ilook 首页
	 * 
	 * @param url
	 * @param classifyId
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request)
			throws Exception {
		Map<String, Object> context = getRootMap();
		return forword("manpower/tabTostHrSalaryinfoT", context);
	}
	/***
	 * 数据展示 人员的全部信息
	 * @param hrid
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/dataList")
	public void dataList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		/*this.init();*/
		SysUser us = SessionUtils.getUser(request);
		// 设置页面数据
		List<TostHrSalaryinfo> dataList = hrsalaryinfotservice
				.getSalaryAllByHrid(us.getId());
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total", dataList.size());
		jsonMap.put("rows", dataList);
		HtmlUtil.writerJson(response, jsonMap);
	}
	@RequestMapping("/test")
	public void test(HttpServletResponse response){
		HtmlUtil.writerJson(response, "");
	}
	
	@RequestMapping("/dataListAll")
	public void dataListAll(TostHrSalaryinfo tostHrSalaryinfo,HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<Integer,Map<String, Float>> map = new HashMap<Integer, Map<String,Float>>();
		//得到开始和结束时间
		String startM = request.getParameter("startMonth");
		String endM = request.getParameter("endMonth");
	    //判断开始和结束时间是否为空。并转换
		if((startM!=null&&startM!=""&&!startM.equalsIgnoreCase("")) &&  
				(endM!=null&&endM!=""&&!endM.equalsIgnoreCase(""))){
			//转换
			 zStartM = DateUtil.getYearMonthByDate(startM);
			 zEndM = DateUtil.getYearMonthByDate(endM);
			//的到选择的类型
			int subsidiestype = Integer.parseInt(request.getParameter("Subsidies"));
			if(subsidiestype!=0){
				if(subsidiestype==1){//补贴
					TostHrSubsidyinfo  stosthrsubsidy = new TostHrSubsidyinfo();
					stosthrsubsidy.setHrId(tostHrSalaryinfo.getHrId());
					stosthrsubsidy.setHsbDepart(tostHrSalaryinfo.getHrDepart());
					Float total = 0.0f;
						 List<TostHrSubsidyinfo> tosthrsubsidyList = hrsubsidyservice.queryAddByTostHrSubsidyinfo(stosthrsubsidy,zStartM,zEndM);
						 int listSize = tosthrsubsidyList.size();
						 Map<String, Object> jsonMap = new HashMap<String, Object>();
						 List<String> monthList = new ArrayList<String>();
						 monthList.add("月份");
						 List<String> hrNameList = new ArrayList<String>();
						 hrNameList.add("XXXX");
						 List<String> subList = new ArrayList<String>();
						 for(int i=0;i<listSize;i++){
							 TostHrSubsidyinfo tosthrsubsidy = tosthrsubsidyList.get(i);
							 hsbEducation = tosthrsubsidy.getHsbEducation()!=null?tosthrsubsidy.getHsbEducation():hsbEducation;
							 hsbComputer = tosthrsubsidy.getHsbComputer()!=null?tosthrsubsidy.getHsbComputer():hsbComputer;
							 hsbKnowadmin = tosthrsubsidy.getHsbKnowadmin()!=null?tosthrsubsidy.getHsbKnowadmin():hsbKnowadmin;
							 hsbExpert = tosthrsubsidy.getHsbExpert()!=null?tosthrsubsidy.getHsbExpert():hsbExpert;
							 hsbTraffic = tosthrsubsidy.getHsbTraffic()!=null?tosthrsubsidy.getHsbTraffic():hsbTraffic;
							 hsbCommunication = tosthrsubsidy.getHsbCommunication()!=null?tosthrsubsidy.getHsbCommunication():hsbCommunication;
							 hsbRemote = tosthrsubsidy.getHsbRemote()!=null?tosthrsubsidy.getHsbRemote():hsbRemote;
							 hsbOthersubs = tosthrsubsidy.getHsbOthersubs()!=null?tosthrsubsidy.getHsbOthersubs():hsbOthersubs;
							 Float totalSub =  hsbEducation + hsbComputer + hsbKnowadmin + hsbExpert + hsbTraffic
									 + hsbCommunication + hsbRemote + hsbOthersubs;
							 String month = tosthrsubsidy.getHsbMonth();
							 String hrName = tosthrsubsidy.getHsbName();
							 int monthListSize = monthList.size();
							 int hrNameListSize = hrNameList.size();
							 total += totalSub;
								 if(!(monthList.get(monthListSize-1).equals(month))){
									 monthList.add(month);
								 }
								 if(!(hrNameList.get(hrNameListSize-1).equals(hrName))){
									 hrNameList.add(hrName);
								 }
						   subList.add(totalSub+"_"+month+"_"+hrName); 	 
						   }
						    jsonMap.put("monthlist", monthList);
						    jsonMap.put("hrNamelist", hrNameList);
						    jsonMap.put("subList", subList);
						    jsonMap.put("resultType", 1);
						    jsonMap.put("total", total);
						    HtmlUtil.writerJson(response, jsonMap);
						    zStartM = null;
							zEndM =null;
			    }else if(subsidiestype==2){//餐补
			    	TostHrMealaddinfo  stosthrmealadd = new TostHrMealaddinfo();
			    	stosthrmealadd.setHrId(tostHrSalaryinfo.getHrId());
			    	stosthrmealadd.setHmeDepartment(tostHrSalaryinfo.getHrDepart());
					Float total = 0.0f;
				    List<TostHrMealaddinfo> tosthrmealaddList= hrmealaddservice.queryMealBytosthrmealadd(stosthrmealadd,zStartM,zEndM);
				    int listSize = tosthrmealaddList.size();
				    List<String> monthList = new ArrayList<String>();
					monthList.add("月份");
					List<String> hrNameList = new ArrayList<String>();
					hrNameList.add("XXXX");
					List<String> hmeMealsubList = new ArrayList<String>();
				    for(int i=0;i<listSize;i++){
				    	TostHrMealaddinfo  tosthrmealadd=tosthrmealaddList.get(i);
				    	hmeMealsubsidies = tosthrmealadd.getHmeMealsubsidies()!=null?tosthrmealadd.getHmeMealsubsidies():hmeMealsubsidies;
				    	int monthListSize = monthList.size();
						int hrNameListSize = hrNameList.size();
						String month = tosthrmealadd.getHmeMonth();
						String hrName = tosthrmealadd.getHmeName();
				    	if(!(monthList.get(monthListSize-1).equals(month))){
				    		monthList.add(month);
				        }
				    	if(!(hrNameList.get(hrNameListSize-1).equals(hrName))){
				    		hrNameList.add(hrName);
				    	}
				    	hmeMealsubList.add(hmeMealsubsidies+"_"+month+"_"+hrName);
				    }
				    Map<String, Object> jsonMap = new HashMap<String, Object>();
				    jsonMap.put("monthlist", monthList);
				    jsonMap.put("hrNamelist", hrNameList);
				    jsonMap.put("hmeMealsubList", hmeMealsubList);
				    jsonMap.put("resultType", 2);
				    jsonMap.put("total", total);
				    HtmlUtil.writerJson(response, jsonMap);
				    zStartM = null;
					zEndM =null;
				}else if(subsidiestype==3){//餐费扣除
					TostHrMealaddinfo  stosthrmealadd = new TostHrMealaddinfo();
			    	stosthrmealadd.setHrId(tostHrSalaryinfo.getHrId());
			    	stosthrmealadd.setHmeDepartment(tostHrSalaryinfo.getHrDepart());
					Float total = 0.0f;
					List<TostHrMealaddinfo> tosthrmealaddList= hrmealaddservice.queryMealBytosthrmealadd(stosthrmealadd,zStartM,zEndM);
					int listSize = tosthrmealaddList.size();
					List<String> monthList = new ArrayList<String>();
					monthList.add("月份");
					List<String> hrNameList = new ArrayList<String>();
					hrNameList.add("XXXX");
					List<String>  hmeMealeduList =new ArrayList<String>();
				    for(int i=0;i<listSize;i++){
				    	TostHrMealaddinfo  tosthrmealadd=tosthrmealaddList.get(i);
				    	hmeMealeduction = tosthrmealadd.getHmeMealeduction()!=null?tosthrmealadd.getHmeMealeduction():hmeMealeduction; 
				    	int monthListSize = monthList.size();
						int hrNameListSize = hrNameList.size();
						String month = tosthrmealadd.getHmeMonth();
						String hrName = tosthrmealadd.getHmeName();
						if(!(monthList.get(monthListSize-1).equals(month))){
				    		monthList.add(month);
				        }
				    	if(!(hrNameList.get(hrNameListSize-1).equals(hrName))){
				    		hrNameList.add(hrName);
				    	}
				    	hmeMealeduList.add(hmeMealeduction+"_"+month+"_"+hrName);
				    }
					Map<String, Object> jsonMap = new HashMap<String, Object>();
					jsonMap.put("monthlist", monthList);
					jsonMap.put("hrNamelist", hrNameList);
					jsonMap.put("hmeMealeduList", hmeMealeduList);
				    jsonMap.put("hrName", tostHrSalaryinfo.getHrName());
				    jsonMap.put("row", tosthrmealaddList);
				    jsonMap.put("resultType", 3);
				    jsonMap.put("total", total);
				    HtmlUtil.writerJson(response, jsonMap);
				    zStartM = null;
					zEndM =null;
				}else if(subsidiestype==4){//社保
					TostHrSocialsecurity stosthrsocialsecurity = new TostHrSocialsecurity();
					stosthrsocialsecurity.setHrId(tostHrSalaryinfo.getHrId());
					Float total = 0.0f;
					List<TostHrSocialsecurity>  tosthrsocialsecurityList =hrsocialsecurityservice.queryScoByHrIdDerpart(stosthrsocialsecurity,tostHrSalaryinfo.getHrDepart(), zStartM, zEndM);
					int listSize = tosthrsocialsecurityList.size();
					List<String> monthList = new ArrayList<String>();
					monthList.add("月份");
					List<String> hrNameList = new ArrayList<String>();
					hrNameList.add("XXXX");
					List<String> socialsecurityList = new ArrayList<String>();
					for(int i =0 ; i<listSize; i++){
						TostHrSocialsecurity tosthrsocialsecurity = tosthrsocialsecurityList.get(i);
						hsoTotal = tosthrsocialsecurity.getHsoTotal()!=null?tosthrsocialsecurity.getHsoTotal():hsoTotal;
						int monthListSize = monthList.size();
						int hrNameListSize = hrNameList.size();
						String month = tosthrsocialsecurity.getHsoMonth();
						String hrName = tosthrsocialsecurity.getHsoName();
						if(!(monthList.get(monthListSize-1).equals(month))){
				    		monthList.add(month);
				        }
				    	if(!(hrNameList.get(hrNameListSize-1).equals(hrName))){
				    		hrNameList.add(hrName);
				    	}
				    	socialsecurityList.add(hsoTotal+"_"+month+"_"+hrName);
					}
					Map<String, Object> jsonMap = new HashMap<String, Object>();
					jsonMap.put("monthlist", monthList);
					jsonMap.put("hrNamelist", hrNameList);
					jsonMap.put("socialsecurityList", socialsecurityList);
				    jsonMap.put("hrName", tostHrSalaryinfo.getHrName());
				    jsonMap.put("row", tosthrsocialsecurityList);
				    jsonMap.put("resultType", 4);
				    jsonMap.put("total", total);
				    HtmlUtil.writerJson(response, jsonMap);
				    zStartM = null;
					zEndM =null;
				}else if(subsidiestype==5){//考勤
					TostHrCheckinlog stosthrcheckinlog = new TostHrCheckinlog();
					stosthrcheckinlog.setHrId(tostHrSalaryinfo.getHrId());
					Float total = 0.0f;
					List<TostHrCheckinlog>	 tosthrcheckinlogList =hrcheckinlogservice.queryCheByHrIdDerpart(stosthrcheckinlog,tostHrSalaryinfo.getHrDepart(),zStartM, zEndM);
					int listSize = tosthrcheckinlogList.size();
					List<String> monthList = new ArrayList<String>();
					monthList.add("月份");
					List<String> hrNameList = new ArrayList<String>();
					hrNameList.add("XXXX");
					List<String> checkinlogList = new ArrayList<String>();
					for(int i =0 ;i<listSize; i++){
						TostHrCheckinlog tosthrcheckinlog = tosthrcheckinlogList.get(i);
						hciDeductionsTotal = tosthrcheckinlog.getHciDeductionsTotal()!=null?tosthrcheckinlog.getHciDeductionsTotal():hciDeductionsTotal;
						int monthListSize = monthList.size();
						int hrNameListSize = hrNameList.size();
						String month = tosthrcheckinlog.getHciMonth();
						String hrName = tosthrcheckinlog.getHrName();
						if(!(monthList.get(monthListSize-1).equals(month))){
				    		monthList.add(month);
				        }
				    	if(!(hrNameList.get(hrNameListSize-1).equals(hrName))){
				    		hrNameList.add(hrName);
				    	}
				    	checkinlogList.add(hciDeductionsTotal+"_"+month+"_"+hrName);
					}
					Map<String, Object> jsonMap = new HashMap<String, Object>();
					jsonMap.put("monthlist", monthList);
					jsonMap.put("hrNamelist", hrNameList);
					jsonMap.put("checkinlogList", checkinlogList);
				    jsonMap.put("hrName", tostHrSalaryinfo.getHrName());
				    jsonMap.put("row", tosthrcheckinlogList);
				    jsonMap.put("resultType", 5);
				    jsonMap.put("total", total);
				    HtmlUtil.writerJson(response, jsonMap);
				    zStartM = null;
					zEndM =null;
				}else if(subsidiestype==6){//工资
					TostHrSalaryinfo stosthrsalaryinfo = new TostHrSalaryinfo();
					stosthrsalaryinfo.setHrId(tostHrSalaryinfo.getHrId());
					Float total = 0.0f;
					List<TostHrSalaryinfo> tosthrsalaryinfoList =tosthrsalaryinfoservice.querySalaryDerpart(stosthrsalaryinfo,tostHrSalaryinfo.getHrDepart(), zStartM, zEndM);
					int listSize = tosthrsalaryinfoList.size();
					List<String> monthList = new ArrayList<String>();
					monthList.add("月份");
					List<String> hrNameList = new ArrayList<String>();
					hrNameList.add("XXXX");
					List<String> salaryinfoList = new ArrayList<String>();
					for(int i =0 ;i<listSize; i++){
						TostHrSalaryinfo tosthrsalaryinfo = tosthrsalaryinfoList.get(i);
						hsaPaid = tosthrsalaryinfo.getHsaPaid()!=null?tosthrsalaryinfo.getHsaPaid():hsaPaid;
						int monthListSize = monthList.size();
						int hrNameListSize = hrNameList.size();
						String month = tosthrsalaryinfo.getHsaMonth();
						List<TostHrBaseinfo> list = tosthrbaseinfoservice.queryAllById(tosthrsalaryinfo.getHrId());
						baseinfo = list.get(0);
						String hrName = baseinfo.getHrName();
						if(!(monthList.get(monthListSize-1).equals(month))){
				    		monthList.add(month);
				        }
				    	if(!(hrNameList.get(hrNameListSize-1).equals(hrName))){
				    		hrNameList.add(hrName);
				    	}
				    	salaryinfoList.add(hsaPaid+"_"+month+"_"+hrName);
					}
					Map<String, Object> jsonMap = new HashMap<String, Object>();
					jsonMap.put("monthlist", monthList);
					jsonMap.put("hrNamelist", hrNameList);
					jsonMap.put("salaryinfoList", salaryinfoList);
				    jsonMap.put("hrName", tostHrSalaryinfo.getHrName());
				    jsonMap.put("row", tosthrsalaryinfoList);
				    jsonMap.put("resultType", 6);
				    jsonMap.put("total", total);
				    HtmlUtil.writerJson(response, jsonMap);
				    zStartM = null;
					zEndM =null;
				}else if(subsidiestype==7){//公积金
					TostHrAccfundinfo stosthraccfundinfo = new TostHrAccfundinfo();
					stosthraccfundinfo.setHrId(tostHrSalaryinfo.getHrId());
					Float total = 0.0f;
					List<TostHrAccfundinfo> tosthraccfundinfoList= tosthraccfundinfoservice.queryAccfundByDerpart(stosthraccfundinfo,tostHrSalaryinfo.getHrDepart(), zStartM, zEndM);
					int listSize = tosthraccfundinfoList.size();
					List<String> monthList = new ArrayList<String>();
					monthList.add("月份");
					List<String> hrNameList = new ArrayList<String>();
					hrNameList.add("XXXX");
					List<String> accfundinfoList = new ArrayList<String>();
					for(int i =0 ;i<listSize; i++){
						TostHrAccfundinfo tosthraccfundinfo = tosthraccfundinfoList.get(i);
						hacFactamount = tosthraccfundinfo.getHacFactamount()!=null?tosthraccfundinfo.getHacFactamount():hacFactamount;
						int monthListSize = monthList.size();
						int hrNameListSize = hrNameList.size();
						String month = tosthraccfundinfo.getHacMonth();
						String hrName = tosthraccfundinfo.getHacName();
						if(!(monthList.get(monthListSize-1).equals(month))){
				    		monthList.add(month);
				        }
				    	if(!(hrNameList.get(hrNameListSize-1).equals(hrName))){
				    		hrNameList.add(hrName);
				    	}
				    	
						accfundinfoList.add(hacFactamount+"_"+month+"_"+hrName);
					}
					Map<String, Object> jsonMap = new HashMap<String, Object>();
					jsonMap.put("monthlist", monthList);
					jsonMap.put("hrNamelist", hrNameList);
					jsonMap.put("accfundinfoList", accfundinfoList);
				    jsonMap.put("hrName", tostHrSalaryinfo.getHrName());
				    jsonMap.put("row", tosthraccfundinfoList);
				    jsonMap.put("resultType", 7);
				    jsonMap.put("total", total);
				    HtmlUtil.writerJson(response, jsonMap);
				    zStartM = null;
					zEndM =null;
				}
			}
			//没有选择类型只选择了开始时间和结束时间
			else {
				sStartM=zStartM;
				sEndM=zEndM;
				datalist = hrsalaryinfoservic.queryByMonth(tostHrSalaryinfo, zStartM, zEndM);
				Map<String, Object> jsonMap = new HashMap<String, Object>();
				jsonMap.put("total",tostHrSalaryinfo.getPager().getRowCount());
				jsonMap.put("rows", datalist);
				HtmlUtil.writerJson(response, jsonMap);
				zStartM = null;
				zEndM =null;
			}
 		}
		//处理选择名字和部门，其他什么没有选或者只选了开始时间或者结束时间 
		else {
		datalist = hrsalaryinfoservic.queryByMonth(tostHrSalaryinfo, zStartM, zEndM);
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("total",tostHrSalaryinfo.getPager().getRowCount());
		jsonMap.put("rows", datalist);
		HtmlUtil.writerJson(response, jsonMap);
		}
	}
	/**
	 * 
	 * @param id
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getById")
	public ModelAndView getById(Integer id, HttpServletRequest request)
			throws Exception {
		Map<String, Object> context = getRootMap();
		Map<String, Object> map = hrsalaryinfotservice.getHrSalaryById(id);
		// 设置页面数据
		context.put("map", map);
		return forword("manpower/tabTostHrSalaryinfoTDetail", context);
	}
    
}



